package hah;

import java.util.Stack;

//用双栈实现队列，一个栈用于存储push的数据，另一个栈用于pop和peek
public class MyQueue {
    Stack<Integer> inStack;
    Stack<Integer> outStack;
    /** Initialize your data structure here. */
    public MyQueue() {
            inStack=new Stack<>();
            outStack=new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        inStack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (outStack.empty()){
            in2out();
        }
        return outStack.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (outStack.empty()){
            in2out();
        }
        return outStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return inStack.empty()&&outStack.empty();
    }
    public void in2out(){
        while(!inStack.empty()){
            outStack.push(inStack.pop());
        }
    }
}